//
//  RefundViewController.m
//  SameWorld(merchant)
//
//  Created by 李小朋 on 2017/4/13.
//  Copyright © 2017年 杨旭. All rights reserved.
//

#import "RefundViewController.h"
#import "OrderManageCell.h"
#import "OrderCalendarView.h"
#import "OrderModel.h"
#import "OrderCommodityModel.h"
@interface RefundViewController ()<UITableViewDelegate ,UITableViewDataSource>
@property (nonatomic ,strong) UserInfo *userInfo;
@property (nonatomic ,strong) UITableView *tableView;
@property (nonatomic ,strong) NSMutableArray *dataSource;
@property (nonatomic ,strong) NSMutableArray *commodityArray;
@property (nonatomic ,strong) NSString *selectTime;
@property (nonatomic ,strong) OrderCommodityModel *commodityModel;
@end

@implementation RefundViewController

- (NSMutableArray *)dataSource {
    if (_dataSource == nil) {
        self.dataSource = [NSMutableArray array];
    }
    return _dataSource;
}
- (NSMutableArray *)commodityArray {
    
    if (!_commodityArray) {
        self.commodityArray = [NSMutableArray array];
    }
    return _commodityArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"退款查询";
    _userInfo = [UserInfo sharedUserInfo];
    [self setNarBarUI];
    [self createTableViewUI];
    [self loadDataOrderList];
}

- (void)loadDataOrderList {
    
    
    //  http://118.190.98.64/tctxAPI/shop_order_list.aspx?zt=1&page=1&shop_bh=1&page_count=8
    NSString *urlStr = [NSString stringWithFormat:@"%@/shop_order_list.aspx",serverUrl];
    NSDictionary *paramDic = @{@"shop_bh":_userInfo.bh,@"zt":@"3",@"page":@"1",@"page_count":@"8"};

    [NetworkManager requestPOSTWithURLStr:urlStr paramDic:paramDic finish:^(id responseObject) {
        //        NSLog(@"请求成功%@",responseObject);
        if (RESPONSEOBJECT) {
            NSArray *dataArr = [responseObject objectForKey:@"data"];
            for (NSDictionary *dict in dataArr) {
                OrderModel *model = [[OrderModel alloc] init];
                [model setValuesForKeysWithDictionary:dict];
                [self.dataSource addObject:model];
                
                NSDictionary *commodityDic = [dict objectForKey:@"data_commodity"];
                self.commodityModel = [OrderCommodityModel mj_objectWithKeyValues:commodityDic];
                [self.commodityArray addObject:self.commodityModel];
                
            }
        }else{
            
            [self showAlertWithTitle:responseObject[@"message"]];
        }
        
        [self.tableView reloadData];
        
    } enError:^(NSError *error) {
        NSLog(@"请求失败%@",error);
    }];
}


- (void)setNarBarUI {
    
    UIBarButtonItem *calendarItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"rili"] style:(UIBarButtonItemStylePlain) target:self action:@selector(calendarItemClicked:)];
    self.navigationItem.rightBarButtonItem = calendarItem;
    
}
#pragma mark - 日历按钮
- (void)calendarItemClicked:(UIBarButtonItem *)sender {
    
    NSLog(@"日历");
    OrderCalendarView *orderCanlendarView = [[OrderCalendarView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [orderCanlendarView showCustomCalendarView];
    orderCanlendarView.selectTime = ^(NSString *time){
        
        self.selectTime = time;
    };
}

- (void)createTableViewUI {

    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, size_width, size_height - 64) style:(UITableViewStylePlain)];
    self.tableView.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"];
    self.tableView.delegate= self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    UIView *headerView = [[UIView alloc] initWithFrame:(CGRectMake(0, 0, size_width, 8))];
    headerView.backgroundColor = [UIColor colorWithHexString:@"f2f2f2"];
    self.tableView.tableHeaderView = headerView;
    
    // 注册cell
    [self.tableView registerNib:[UINib nibWithNibName:@"OrderManageCell" bundle:nil] forCellReuseIdentifier:@"OrderManageCell"];
}

#pragma mark - UITableView代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return self.dataSource.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    OrderManageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OrderManageCell" forIndexPath:indexPath];
    OrderModel *model = [self.dataSource objectAtIndex:indexPath.section];
    OrderCommodityModel *commodityModel = [self.commodityArray objectAtIndex:indexPath.section];
    [cell setUpDataWithOrderModel:model withType:@"3"];
    [cell setUpDataWtihrderCommodityModel:commodityModel];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return 222;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 8;
}




@end
